package com.qq.googleplay.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * ============================================================
 * 版 权 ： Google互联网有限公司版权所有 (c) 2016
 * 作 者 : 陈冠杰
 * 版 本 ： 1.0
 * 创建日期 ：2016/3/20 17:01
 * 描 述 ：基类adapter
 * 修订历史 ：
 * ============================================================
 **/
public abstract class CommonAdapter<T> extends BaseAdapter {
    protected Context mContext;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;
    private int layoutId;
    private int layoutId_LoadMore;

    public static final int VIEWTYPE_LOADMORE = 0;
    public static final int VIEWTYPE_NORMAL = 1;

    public CommonAdapter(Context context, List<T> datas, int layoutId_LoadMore, int layoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mDatas = datas;
        this.layoutId = layoutId;
        this.layoutId_LoadMore = layoutId_LoadMore;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (getItemViewType(position) == VIEWTYPE_LOADMORE) {
            holder = ViewHolder.get(mContext, convertView, parent,
                    layoutId_LoadMore, position);
            convertLoadMore(holder, getItem(position));
        } else {
            holder = ViewHolder.get(mContext, convertView, parent,
                    layoutId, position);
            convert(holder, getItem(position));
        }

        return holder.getConvertView();
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == getCount() - 1) {
            return VIEWTYPE_LOADMORE;
        }
        return VIEWTYPE_NORMAL;
    }


    public abstract void convert(ViewHolder holder, T t);
    public abstract void convertLoadMore(ViewHolder holder, T t);

}
